layui.define(['jquery', 'tool'], function (exports) {
    var $ = layui.jquery
        , tool = layui.tool
        , loading = tool.loading
        , msg = tool.msg;
    var request = {
        get: function (options, _callback) {
            options.type = 'get';
            request.ajax(options, _callback);
        }
        , post: function (options, _callback) {
            options.type = 'post';
            if (options.data !== false)
                request.ajax(options, _callback);
        }
        , put: function (options, _callback) {
            options.type = 'put';
            request.ajax(options, _callback);
        }, delete: function (options, _callback) {
            options.type = 'delete';
            request.ajax(options, _callback);
        }
        , ajax: function (options, _callback) {
            if (options.load === undefined || options.load === true)
                loading.Load(3, "请稍后..");
            $.ajax({
                url: options.url
                ,
                type: options.type
                ,
                data: (options.type === 'post' || options.type === 'put') ? JSON.stringify(options.data === undefined ? {} : options.data) : ''
                ,
                dataTypeString: 'json'
                ,
                contentType: 'application/json;charset=utf-8'
                ,
                success: function (res) {
                    if (options.load === undefined || options.load === true)
                        loading.remove(500);
                    if (options.msg === undefined || options.msg === true)
                        if (res.code === 0)
                            msg.success(res.message === undefined ? "成功" : res.message);
                        else
                            msg.error(res.message === undefined ? "失败" : res.message);
                    if (typeof _callback == 'function')
                        _callback(res);
                }
                ,
                error: function (XMLHttpRequest, textStatus, err) {
                    loading.remove(500);
                    msg.error("服务器错误")
                    console.log('ajax Error XMLHttpRequest: ' + JSON.stringify(XMLHttpRequest))
                    console.log('ajax Error textStatus : ' + textStatus)
                    console.log('ajax Error: ' + JSON.stringify(err))
                    console.log('ajax Error url: ' + options.url)
                    console.log('ajax Error data: ' + JSON.stringify(options.data))
                }
            })
        }
    };
    exports('request', request)
});